package com.rp.configure.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.Selectway;
import com.rp.configure.entity.Gateway;
import com.rp.configure.service.GatewayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/gateway")
@Api(tags="网关管理")
public class GatewayController {
    @Autowired
    private GatewayService gatewayService;

    @Autowired
    private Selectway selectway;

    @ApiOperation(value = "分页列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list_page")
    public ResultDTO<IPage<Gateway>> list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                          @RequestParam(required = false) @ApiParam(value = "模糊查询字符串") String string) {
        QueryWrapper<Gateway> queryWrapper = new QueryWrapper<Gateway>();
        selectway.fields(queryWrapper, string,"gateway",null,null);
        queryWrapper.orderByDesc("create_time");
        return ResultDTO.success().data(gatewayService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @ApiOperation(value = "分页列表通过项目id,用户id,在线状态查询")
    @GetMapping(value = "/list_user")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO<IPage<Gateway>> listByUser(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                                @RequestParam(value = "userId",required = false) String userId,
                                                @RequestParam(value = "subjectId",required = false) String subject,
                                                @RequestParam(value = "isOnline",required = false) Integer isOnline) {
        Page<Gateway> page = new Page<>(pageNum, pageSize);
        IPage<Gateway> listPage=gatewayService.listPage(page,userId,subject,isOnline);
        return ResultDTO.success().data(listPage);
    }

    @ApiOperation(value = "分页列表通过项目id")
    @GetMapping(value = "/list_subject")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO<List<Gateway>> listByUser(@RequestParam(value = "subjectId") String subjectId) {
        QueryWrapper<Gateway> wrapper = new QueryWrapper<>();
        wrapper.eq("subject_id",subjectId);

        return ResultDTO.success().data(gatewayService.list(wrapper));
    }
    @PostMapping("/insert")
    @ApiOperation(value = "增加")
    @Transactional
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO insert(@RequestBody Gateway gateway) {
        return ResultDTO.tf(gatewayService.save(gateway));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO update(@RequestBody Gateway gateway) {
        return ResultDTO.tf(gatewayService.updateById(gateway));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id) {
        return ResultDTO.tf(gatewayService.removeById(id));
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids) {
        return ResultDTO.tf(gatewayService.removeByIds(Arrays.asList(ids.split(","))));
    }


}
